package com.qbw.app.module.common.fragment;

import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;

import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.CommonFragmentInputBinding;
import com.qbw.core.observable.ObservableString;

/**
 * @author QBW
 * @date 2019/9/27
 */
public class InputDialogFragment extends CommonDialogFragment<CommonFragmentInputBinding> {

    private int mType;
    private String mTitle;
    private String mHint;
    private String mPositiveButtonText;
    private ObservableString mInput = new ObservableString();
    private int mMaxLength;
    private String mDefaultValue;

    public static InputDialogFragment createInstance(int type, String title, String hint) {
        return createInstance(type, title, hint, "", "", 0);
    }

    public static InputDialogFragment createInstance(int type,
                                                     String title,
                                                     String hint,
                                                     int maxLength) {
        return createInstance(type, title, hint, "", "", maxLength);
    }

    public static InputDialogFragment createInstance(int type,
                                                     String title,
                                                     String hint,
                                                     String positiveText,
                                                     String defaultValue,
                                                     int maxLength) {
        InputDialogFragment ins = new InputDialogFragment();
        Bundle args = new Bundle();
        args.putInt(Constant.KEY_TYPE, type);
        args.putString(Constant.KEY_TITLE, title);
        args.putString(Constant.KEY_HINT, hint);
        args.putString(Constant.KEY_POSITIVE_BUTTON_TEXT, positiveText);
        args.putString(Constant.KEY_DEFAULT_VALUE, defaultValue);
        args.putInt(Constant.KEY_MAX_LENGTH, maxLength);
        ins.setArguments(args);
        return ins;
    }

    @Override
    protected int onGetLayoutId() {
        return R.layout.common_fragment_input;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
        mType = args.getInt(Constant.KEY_TYPE);
        mTitle = args.getString(Constant.KEY_TITLE);
        mHint = args.getString(Constant.KEY_HINT);
        mPositiveButtonText = args.getString(Constant.KEY_POSITIVE_BUTTON_TEXT);
        mDefaultValue = args.getString(Constant.KEY_DEFAULT_VALUE);
        mMaxLength = args.getInt(Constant.KEY_MAX_LENGTH, 0);
        mInput.setValue(mDefaultValue);
        if (mMaxLength > 0) {
            mBinding.etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mMaxLength)});
        }
        mBinding.tvTitle.setText(mTitle);
        mBinding.etInput.setHint(mHint);
        mBinding.setInput(mInput);
        if (!TextUtils.isEmpty(mPositiveButtonText)) {
            mBinding.tvSure.setText(mPositiveButtonText);
        }
        mBinding.tvSure.setOnClickListener(this);
        mBinding.tvCancel.setOnClickListener(this);
        mBinding.executePendingBindings();
        int inputLen = mBinding.etInput.getText().length();
        if (inputLen > 0) {
            mBinding.etInput.setSelection(inputLen);
        }
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_sure) {
            Listener listener = getListener();
            if (listener != null) {
                listener.onClickInputOk(mType, mInput.getValue());
            }
            dismiss();
        } else if (vid == R.id.tv_cancel) {
            Listener listener = getListener();
            if (listener != null) {
                listener.onClickInputCancel(mType);
            }
            dismiss();
        }
    }

    private Listener getListener() {
        Listener listener = null;
        Fragment fragment = getParentFragment();
        if (fragment instanceof Listener) {
            listener = (Listener) fragment;
        } else {
            Activity activity = getActivity();
            if (activity instanceof Listener) {
                listener = (Listener) activity;
            }
        }
        return listener;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(Constant.KEY_TYPE, mType);
        outState.putString(Constant.KEY_TITLE, mTitle);
        outState.putString(Constant.KEY_HINT, mHint);
        outState.putString(Constant.KEY_POSITIVE_BUTTON_TEXT, mPositiveButtonText);
        outState.putString(Constant.KEY_DEFAULT_VALUE, mDefaultValue);
        outState.putInt(Constant.KEY_MAX_LENGTH, mMaxLength);
    }

    public interface Listener {
        void onClickInputOk(int type, String inputContent);

        void onClickInputCancel(int type);
    }
}
